//
//  CompetitionList.m
//  YOGProj
//
//  Created by liyun on 14-7-1.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "CompetitionList.h"
#import "competionListCustomCell.h"
#import "competionListEnCustomCell.h"
#import "MacroDefinition.h"

@interface CompetitionList ()<UITableViewDataSource,UITableViewDelegate>
{
    BOOL en;
    IBOutlet UITableView *competionListTable;
}

@end

@implementation CompetitionList
@synthesize competionArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if(!competionArray)
            competionArray = [[NSArray alloc] init];
    }
    return self;
}

- (void)navigationShowLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 
    self.title = NSLocalizedString(@"facility_event", @"");
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:NAVIGATION_BACK_IMAGE] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(navigationShowLeft) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItemleft=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem=barItemleft;
    
    UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(navigationShowLeft)];
    recognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:recognizerRight];
    
    competionListTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    NSLog(@"%@",currentLanguage);
    
    if([currentLanguage isEqualToString:@"zh-Hans"])
        en = false;
    else
        en = true;
}


#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(en)
        return 130;
    else
        return 86;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return competionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(en)
    {
        competionListEnCustomCell* cell = (competionListEnCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"competionListEnCustomCell"];
        if (cell == nil)
        {
            NSArray* arry = [[NSBundle mainBundle] loadNibNamed:@"competionListEnCustomCell" owner:nil options:nil];
            cell = [arry objectAtIndex:0];
        }
        
        UIImageView *image  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 129, 320, 1)];
        image.backgroundColor = TABLE_LINE;
        [cell.contentView addSubview:image];
        
        NSLog(@"%@--%@",[competionArray[indexPath.row] objectForKey:@"sportCode"],[competionArray[indexPath.row] objectForKey:@"sportName"]);
        
        cell.image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[competionArray[indexPath.row] objectForKey:@"sportCode"]]];
        cell.event.text = [NSString stringWithFormat:@"%@",[competionArray[indexPath.row] objectForKey:@"eventNameEn"]];
        cell.competion.text = [NSString stringWithFormat:@"Phase：%@",[competionArray[indexPath.row] objectForKey:@"compPhaseEn"]];
        NSString *time = [NSString stringWithFormat:@"Date&Time：%@ %@-%@",[competionArray[indexPath.row] objectForKey:@"compDate"],[competionArray[indexPath.row] objectForKey:@"startTime"],[competionArray[indexPath.row] objectForKey:@"endTime"]];
        cell.time.text = time;
        //    cell.date.text = [NSString stringWithFormat:@"比赛日期：%@",];
        cell.address.text = [NSString stringWithFormat:@"Venue：%@",[competionArray[indexPath.row] objectForKey:@"venueNameEn"]];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        competionListCustomCell* cell = (competionListCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"competionListCustomCell"];
        if (cell == nil)
        {
            NSArray* arry = [[NSBundle mainBundle] loadNibNamed:@"competionListCustomCell" owner:nil options:nil];
            cell = [arry objectAtIndex:0];
        }
        
        UIImageView *image  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 85, 320, 1)];
        image.backgroundColor = TABLE_LINE;
        [cell.contentView addSubview:image];
        
        NSLog(@"%@--%@",[competionArray[indexPath.row] objectForKey:@"sportCode"],[competionArray[indexPath.row] objectForKey:@"sportName"]);
        
        cell.image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[competionArray[indexPath.row] objectForKey:@"sportCode"]]];
        cell.event.text = [NSString stringWithFormat:@"%@",[competionArray[indexPath.row] objectForKey:@"eventName"]];
        cell.competion.text = [NSString stringWithFormat:@"比赛项目：%@",[competionArray[indexPath.row] objectForKey:@"compPhase"]];
        NSString *time = [NSString stringWithFormat:@"比赛时间：%@ %@-%@",[competionArray[indexPath.row] objectForKey:@"compDate"],[competionArray[indexPath.row] objectForKey:@"startTime"],[competionArray[indexPath.row] objectForKey:@"endTime"]];
        cell.time.text = time;
        //    cell.date.text = [NSString stringWithFormat:@"比赛日期：%@",];
        cell.address.text = [NSString stringWithFormat:@"比赛地点：%@",[competionArray[indexPath.row] objectForKey:@"venueName"]];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
